import java.util.ArrayList;
import java.util.List;

/**
 * 1974. 使用特殊打字机键入单词的最少时间
 * https://leetcode-cn.com/problems/minimum-time-to-type-word-using-special-typewriter/
 */
public class Solutions_1974 {
    public static void main(String[] args) {
        String word1 = "abc";  // output: 5
        String word2 = "bza";  // output: 7
        String word3 = "zjpc";  // output: 34

        List<String> inputs = new ArrayList<>();
        inputs.add(word1);
        inputs.add(word2);
        inputs.add(word3);

        for (String input : inputs) {
            int result = minTimeToType(input);
            System.out.println(result);
        }
    }

    public static int minTimeToType(String word) {
        int res = 0;
        char[] arr = word.toCharArray();
        char prev = 'a';
        for (char cur : arr) {
            int count1 = Math.abs(cur - prev);
            int count2 = 26 - count1;
            // Math.min(由 last 位置顺时钟旋转到 c 需要的次数, 由 last 位置逆时钟旋转到 c 需要的次数)
            res += Math.min(count1, count2);
            prev = cur;
        }
        return res + arr.length;
    }
}
